﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Metadata;

namespace Sino.CapacityCloud.Repositories.Models
{

    public class ApplicationDbContext
        : IdentityDbContext<ApplicationUser, ApplicationRole, string>
    {

        #region Common
        public DbSet<SmsCode> SmsCodes { get; set; }

        public DbSet<WebApiCredential> WebApiCredentials { get; set; }
        #endregion

        #region User
        public DbSet<UserPointRecord> UserPoints { get; set; }

        public DbSet<Carrier> Carriers { get; set; }

        public DbSet<RatingRecord> Ratings { get; set; }
        #endregion

        #region Wallet
        public DbSet<BankCard> BankCards { get; set; }

        public DbSet<Bank> Banks { get; set; }
        #endregion

        #region Chat
        public DbSet<FriendRelationship> FriendRelationships { get; set; }
        #endregion

        #region Cost
        public DbSet<PayableCost> PayableCosts { get; set; }

        public DbSet<Cost> Costs { get; set; }

        public DbSet<PaidCost> PaidCosts { get; set; }

        public DbSet<PaymentHistory> PaymentHistories { get; set; }
        #endregion

        #region Location
        public DbSet<AccidentPicture> AccidentPictures { get; set; }

        public DbSet<Location> Locations { get; set; }
        #endregion

        #region Order
        public DbSet<Order> Orders { get; set; }

        public DbSet<ConfirmedOrder> ConfirmedOrders { get; set; }

        public DbSet<Quotation> Quotations { get; set; }

        public DbSet<UserFocusedConfirmedOrder> UserFocusedOrders { get; set; }
        #endregion

        #region Transportion
        public DbSet<Car> Cars { get; set; }

        public DbSet<CarAddingModerationCommit> CarAddingModerationCommits { get; set; }

        public DbSet<TransportLinkageDispacherTask> TransportLinkageDispacherTasks { get; set; }

        public DbSet<TransportLinkage> TransportLinkages { get; set; }

        public DbSet<FavouriteTransportLinkage> FavouriteTransportLinkages { get; set; }

        public DbSet<CommonTransportLinkage> CommonTransportLinkages { get; set; }
        #endregion

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);

            builder.Entity<ApplicationUser>(c => c.Ignore(p => p.OverallCertificateStatus));

            builder.Entity<Order>(c =>
            {
                c.HasMany(p => p.SubOrders).WithOne(p => p.Parent);
            });

            builder.Entity<ConfirmedOrder>(c =>
            {
                c.HasMany(p => p.SubOrders).WithOne(p => p.Parent);
            });

            builder.Entity<CommonTransportLinkage>()
                .HasOne(p => p.TransportLinkage)
                .WithMany()
                .IsRequired(false);

            builder.Entity<FavouriteTransportLinkage>()
                .HasOne(p => p.TransportLinkage)
                .WithMany()
                .IsRequired(false);

            //builder.Entity<BankCard>().HasAlternateKey(x => x.Number);
            //builder.Entity<Car>(c =>
            //{
            //    c.HasAnnotation("MaxLength", 256);
            //    c.HasIndex(x => x.Code);
            //    c.HasAlternateKey(x => x.Code);
            //});

            //builder.Entity<ApplicationUser>(c =>
            //{
            //    c.HasAnnotation("MaxLength", 256);
            //    c.HasIndex(x => x.PhoneNumber);
            //    c.HasAlternateKey(x => x.PhoneNumber);
            //});
        }

    }

}